﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 5. Write a function that finds the youngest person in a 
			given array of persons and prints his/hers full name
			* Each person have properties firstname, lastname and 
			age, as shown: 
			var persons = [
				{firstname : "Gosho", lastname: "Petrov", age: 32}, 
				{firstname : "Bay", lastname: "Ivan", age: 81},…]; -->
	<style>
		strong {
			text-decoration: underline;
		}
	</style>
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// Finds the youngest person index
		function findMinAge(persons) {
			var minIndex = -1;

			if (persons.length > 0) {
				var minAge = persons[0].age;
				var minIndex = 0;

				for (var i = 1; i < persons.length; i++) {
					if (minAge > persons[i].age) {
						minAge = persons[i].age;
						minIndex = i;
					}
				}
			}

			return minIndex;
		}

		// Array with JASON persons
		var persons = [
						{ firstname: "Gosho", lastname: "Petrov", age: 32 },
						{ firstname: "Pesho", lastname: "Dimitrov", age: 18 },
						{ firstname: "Bay", lastname: "Ivan", age: 81 },
						{ firstname: "Baby", lastname: "Ivanov", age: 1 },
						{ firstname: "Baby", lastname: "Petrov", age: 2 }
						];

		// All persons
		jsConsole.writeLine("<strong>All persons</strong>");
		for (var i in persons) {
			jsConsole.writeLine(persons[i].firstname + " " +
								persons[i].lastname + " age: " +
								persons[i].age);
		}
		jsConsole.writeLine();

		// Find youngest person
		var person = persons[findMinAge(persons)];

		jsConsole.writeLine("<strong>Result</strong>");
		jsConsole.writeLine("The youngest person is: " +
								person.firstname + ' ' +
								person.lastname);
	</script>
</body>
</html>
